package com.example.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.example.domain.fanweiKQ.JbtsEntity;
import com.example.domain.vo.JbtsExcel;
import com.example.domain.xsrb.OAXsrbFinalEntity;
import com.example.domain.xsrb.OAXsrbMainEntity;
import com.example.domain.xsrb.OAXsrbTodayEntity;
import com.example.domain.xsrb.OAXsrbYesterdayEntity;
import com.example.mapper.OAXsrbMapper;
import com.example.service.OAXsrbService;
import com.example.utils.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
@Slf4j
public class OAXsrbServiceImp implements OAXsrbService {

	@Autowired
	OAXsrbMapper oaXsrbMapper;

	@Override
	public List<OAXsrbFinalEntity> queryXsrbMain() {
		List<OAXsrbFinalEntity> finalList = new ArrayList<>();
		//主日报
		List<OAXsrbMainEntity> list = oaXsrbMapper.queryXsrbMain();
		for (OAXsrbMainEntity item: list) {
			OAXsrbFinalEntity oaXsrbFinalEntity = new OAXsrbFinalEntity();
			oaXsrbFinalEntity.setUserName(item.getUserName()); //销售经理
			oaXsrbFinalEntity.setDepartmentName(item.getDepartmentName()); //部门
			oaXsrbFinalEntity.setRq(item.getRq()); //日期
			oaXsrbFinalEntity.setWz(item.getWz()); //位置

			// 销售日报 昨日
			List<OAXsrbYesterdayEntity> yesterdayList = oaXsrbMapper.queryXsrbYesterDay(item.getId());
			StringBuilder yesterDaySb = new StringBuilder();
			for (OAXsrbYesterdayEntity yesterDayItem:yesterdayList) {
				yesterDaySb.append("客户：");
				yesterDaySb.append(yesterDayItem.getKhName());
				yesterDaySb.append("<br>");
				yesterDaySb.append("项目：");
				yesterDaySb.append(yesterDayItem.getXmName());
				yesterDaySb.append("<br>");
				yesterDaySb.append("计划内容：");
				yesterDaySb.append(yesterDayItem.getJhnr());
				yesterDaySb.append("<br>");
				yesterDaySb.append("完成结果：");
				yesterDaySb.append(yesterDayItem.getWcqk());
				yesterDaySb.append("<br>");
			}
			String yesterDayStr = yesterDaySb.toString();
			oaXsrbFinalEntity.setZr(yesterDayStr.substring(0,yesterDayStr.length() - 5));
			// 销售日报 今日
			List<OAXsrbTodayEntity> todayList = oaXsrbMapper.queryXsrbToday(item.getId());
			StringBuilder todayDaySb = new StringBuilder();
			for (OAXsrbTodayEntity todayItem:todayList) {
				todayDaySb.append("客户：");
				todayDaySb.append(todayItem.getKhName());
				todayDaySb.append("<br>");
				todayDaySb.append("项目：");
				todayDaySb.append(todayItem.getXmName());
				todayDaySb.append("<br>");
				todayDaySb.append("计划内容：");
				todayDaySb.append(todayItem.getNr());
				todayDaySb.append("<br>");
			}
			String toDayStr = todayDaySb.toString();
			oaXsrbFinalEntity.setJr(toDayStr.substring(0,toDayStr.length() - 4));
			finalList.add(oaXsrbFinalEntity);
		}

		return finalList;
	}

	@Override
	public String xsrbExport() {
		String name = "xsrb" + System.currentTimeMillis() + ".xlsx";
		String fileName = CommonConfig.getProfile() + name;
		List<OAXsrbFinalEntity> jblist = queryXsrbMain();
		for (OAXsrbFinalEntity item:jblist) {
			String jrStr = item.getJr().replaceAll("<br>","\n").replaceAll("&nbsp;","");
			String zrStr = item.getZr().replaceAll("<br>","\n").replaceAll("&nbsp;","");
			item.setZr(zrStr);
			item.setJr(jrStr);
		}
		//设置自动换行
		WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
		contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
		contentWriteCellStyle.setWrapped(true);
		//头策略使用默认 设置字体大小
		WriteCellStyle headWriteCellStyle = new WriteCellStyle();
		// 写入数据
		EasyExcel.write(fileName, OAXsrbFinalEntity.class)
				.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle))
				.sheet().doWrite(jblist);
		return name;
	}


}
